package com.znsd.zl.app.ocr.utils;

import com.baidu.aip.ocr.AipOcr;
import com.baidu.aip.ocr.OcrConsts;
import com.znsd.zl.app.ocr.entity.IdCardBackEntity;
import com.znsd.zl.app.ocr.entity.IdCardEntity;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *     身份证识别工具类
 * </p>
 *
 * @ClassName IdCardUtils
 * @Description 百度OCR文字识别工具类
 * @Author jaypdd
 * @Date 2020/8/29 15:49
 * @Version v1.0
 */
@Component
@Slf4j
public class IdCardUtils {

	@Resource
	private AipOcr aipOcr;

	@Resource
	private JsonUtils jsonUtil;

	/**
	 * TODO 弃用
	 * 身份证证件照正面识别
	 * @param bytes     证件照字节数组
	 * @return
	 */
	public IdCardEntity idCardFrontSample(byte[] bytes){
		// 开启身份证风险类型识别
		Map<String, String> map = new HashMap<String, String>(1);
		map.put("detect_risk", "true");
		JSONObject res = aipOcr.idcard(bytes, "front", new HashMap<String, String>(1));
		log.info("【百度OCR】身份证正面识别状态信息：{}", res.toString());
		IdCardEntity idCardEntity = jsonUtil.idCardFront(res);
		return idCardEntity;
	}

	/**
	 * TODO 弃用
	 * 身份证证件照背面识别
	 * @param bytes     证件照字节数组
	 * @return
	 */
	public IdCardBackEntity idCardBackSample(byte[] bytes){
		Map<String, String> map = new HashMap<String, String>(1);
		map.put("detect_risk", "true");
		JSONObject res = aipOcr.idcard(bytes, "back", new HashMap<String, String>(1));
		log.info("【百度OCR】身份证背面识别状态信息：{}", res.toString());
		IdCardBackEntity idCardBackEntity = jsonUtil.idCardBackJson(res);
		return idCardBackEntity;
	}

}
